package com.oa.ylms.apps.business.archives.impl;

import java.util.List;

import com.oa.ylms.apps.business.archives.IArchiveInfoService;
import com.oa.ylms.apps.business.archives.IAvatarInfoService;
import com.oa.ylms.apps.business.archives.IMailInfoService;
import com.oa.ylms.apps.business.archives.IMajorInfoService;
import com.oa.ylms.apps.business.organization.IPostInfoService;
import com.oa.ylms.apps.common.DateUtil;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.dao.archives.IArchiveInfoDao;
import com.oa.ylms.apps.entity.archives.ArchiveInfoValue;

public class ArchiveInfoServiceImpl implements IArchiveInfoService {
	private IMailInfoService mailInfoService;
	private IMajorInfoService majorInfoService;
	private IAvatarInfoService avatarInfoService;
	private IPostInfoService postInfoService;
	private IArchiveInfoDao infoDao;
	private ArchiveInfoValue archiveInfo;
	private List<ArchiveInfoValue> archiveList;
	
	@Override
	public int addArchiveInfo(int mailId, int majorId, int avatarId,
			int postId, String staffName, String gender, int age,
			String birthday, String politician, String grade, String classNo,
			String telphone, String dormitory,String archiveNo) throws Exception {
		archiveInfo.setMailInfo(mailInfoService.queryByMailId(mailId));
		archiveInfo.setMajorInfo(majorInfoService.queryByMajorId(majorId));
		archiveInfo.setAvatarInfo(avatarInfoService.queryByAvatarId(avatarId));
		archiveInfo.setPostInfo(postInfoService.queryByPostId(postId));
		if(archiveInfo.getMailInfo() != null && archiveInfo.getMajorInfo() != null && archiveInfo.getAvatarInfo() != null && archiveInfo.getPostInfo() != null){
			archiveInfo.setStaffName(staffName);
			archiveInfo.setArchiveNo(archiveNo);
			archiveInfo.setGender(gender);
			archiveInfo.setAge(age);
			archiveInfo.setBirthday(DateUtil.toDateWithoutHours(birthday));
			archiveInfo.setPolitician(politician);
			archiveInfo.setGrade(grade);
			archiveInfo.setClassNo(classNo);
			archiveInfo.setTelphone(telphone);
			archiveInfo.setDormitory(dormitory);
			archiveInfo.setStates(IDEDStates.ENABLE);
			return infoDao.addArchiveInfo(archiveInfo);
		}
		return 0;
	}

	@Override
	public boolean modifyArchiveInfo(int archiveId, int majorId, String staffName, int age, String politician, String grade, String classNo,String telphone, String dormitory,int mailId, String mail) throws Exception {
		archiveInfo = queryByArchiveId(archiveId);
		if(archiveInfo != null){
			archiveInfo.setMajorInfo(majorInfoService.queryByMajorId(majorId));
			if(!archiveInfo.getMailInfo().getMail().equals(mail)){
				mailInfoService.modifyMailInfo(mailId, mail);
			}
			if(archiveInfo.getMajorInfo() != null){
				archiveInfo.setStaffName(staffName);
				archiveInfo.setAge(age);
				archiveInfo.setPolitician(politician);
				archiveInfo.setGrade(grade);
				archiveInfo.setClassNo(classNo);
				archiveInfo.setTelphone(telphone);
				archiveInfo.setDormitory(dormitory);
				return infoDao.editArchiveInfo(archiveInfo);
			}
		}
		return false;
	}
	
	@Override
	public boolean modifyArchiveInfo(int archiveId, int majorId,
			String staffName, int age, String birthday, String politician,
			String grade, String classNo, String telphone, String dormitory,
			String archiveNo) throws Exception {
		archiveInfo = queryByArchiveId(archiveId);
		if(archiveInfo != null){
			archiveInfo.setMajorInfo(majorInfoService.queryByMajorId(majorId));
			if(archiveInfo.getMajorInfo() != null){
				archiveInfo.setStaffName(staffName);
				archiveInfo.setAge(age);
				archiveInfo.setBirthday(DateUtil.toDateWithoutHours(birthday));
				archiveInfo.setPolitician(politician);
				archiveInfo.setGrade(grade);
				archiveInfo.setClassNo(classNo);
				archiveInfo.setTelphone(telphone);
				archiveInfo.setDormitory(dormitory);
				archiveInfo.setArchiveNo(archiveNo);
				return infoDao.editArchiveInfo(archiveInfo);
			}
		}
		return false;
	}

	@Override
	public ArchiveInfoValue queryByArchiveId(int archiveId)
			throws Exception {
		archiveInfo.setArchiveId(archiveId);
		return infoDao.queryByArchiveId(archiveInfo);
	}

	@Override
	public boolean toDisable(int archiveId) throws Exception {
		archiveInfo = queryByArchiveId(archiveId);
		if(archiveInfo != null){
			archiveInfo.setStates(IDEDStates.DISABLE);
			return infoDao.editArchiveInfo(archiveInfo);
		}
		return false;
	}

	@Override
	public boolean toEnable(int archiveId) throws Exception {
		archiveInfo = queryByArchiveId(archiveId);
		if(archiveInfo != null){
			archiveInfo.setStates(IDEDStates.ENABLE);
			return infoDao.editArchiveInfo(archiveInfo);
		}
		return false;
	}
	
	@Override
	public String queryArchiveNo(String archiveNo) throws Exception {
		archiveInfo.setArchiveNo(archiveNo);
		archiveList = infoDao.queryArchiveNo(archiveInfo);
		if(archiveList != null){
			for(ArchiveInfoValue aiv:archiveList){
				return aiv.getArchiveNo();
			}
		}
		return null;
	}
	
	public IMailInfoService getMailInfoService() {
		return mailInfoService;
	}

	public void setMailInfoService(IMailInfoService mailInfoService) {
		this.mailInfoService = mailInfoService;
	}

	public IMajorInfoService getMajorInfoService() {
		return majorInfoService;
	}

	public void setMajorInfoService(IMajorInfoService majorInfoService) {
		this.majorInfoService = majorInfoService;
	}

	public IAvatarInfoService getAvatarInfoService() {
		return avatarInfoService;
	}

	public void setAvatarInfoService(IAvatarInfoService avatarInfoService) {
		this.avatarInfoService = avatarInfoService;
	}

	public IPostInfoService getPostInfoService() {
		return postInfoService;
	}

	public void setPostInfoService(IPostInfoService postInfoService) {
		this.postInfoService = postInfoService;
	}

	public IArchiveInfoDao getInfoDao() {
		return infoDao;
	}

	public void setInfoDao(IArchiveInfoDao infoDao) {
		this.infoDao = infoDao;
	}

	public ArchiveInfoValue getArchiveInfo() {
		return archiveInfo;
	}

	public void setArchiveInfo(ArchiveInfoValue archiveInfo) {
		this.archiveInfo = archiveInfo;
	}

	public void setArchiveList(List<ArchiveInfoValue> archiveList) {
		this.archiveList = archiveList;
	}

	public List<ArchiveInfoValue> getArchiveList() {
		return archiveList;
	}

	@Override
	public boolean modifyArchiveInfo(ArchiveInfoValue archiveInfo)
			throws Exception {
		return infoDao.editArchiveInfo(archiveInfo);
	}

}
